<?php
class Blog_Block_Article extends Epic_View_Block 
{	
	public function createPagination($categoryId = null)
	{
		$sql = 'SELECT COUNT(*) AS cnt FROM `blog_articles` WHERE `active` = 1';
		if ($categoryId)
			$sql .= ' AND category_id = ' . (int) $categoryId;
		$totalArticles = Epic::getAdapter()->query($sql)->fetchOne();
		
		parent::createPagination($totalArticles['cnt']);
		//$this->getPagination()->setItemsPerPage(2)->createPagination();
	}
	
	/**
	 * Get all articles by criteria (active, category_id)
	 * 
	 * @param int $categoryId criteria by id category
	 * @return Epic_Db_Collection all rows retrive form database
	 */
	public function getArticles($categoryId = null) 
	{
		$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
		$itemsPerPage = 10;
		$offset = ($currentPage - 1) * $itemsPerPage;
		
 		$this->createPagination($categoryId);
		
		if ($categoryId) {
			return Epic::getModel('blog/article')->getCollection()
				   ->where('category_id', array('eq' => (int) $categoryId))
				   ->where('active', array('eq' => 1))
				   ->order('article_id')
				   ->limit($itemsPerPage, $offset)
				   ->load();
		}
		return Epic::getModel('blog/article')->getCollection()
					->where('active', array('eq' => 1))
					->order('article_id')
					->limit($itemsPerPage, $offset)
					->load();
	}
	
	/**
	 * Get all articles
	 * 
	 * @return Epic_Db_Collection all rows retrive form database
	 */
	public function getAllArticles() 
	{
		$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
		$itemsPerPage = 10;
		$offset = ($currentPage - 1) * $itemsPerPage;
		
		$this->createPagination();
		
		return Epic::getModel('blog/article')->getCollection()
						->order('article_id')
						->limit($itemsPerPage, $offset)
						->load();
	}
	
	/**
	 * Get new articles
	 *
	 * @return Epic_Db_Collection new row retrive form database
	 */
	public function getNewArticles()
	{
		return Epic::getModel('blog/article')->getCollection()
			->order('article_id')
			->limit(6)
			->load();
	}
	
	/**
	 * Get number comments by article id
	 * 
	 * @param int $articleId article id
	 * @return int number comments
	 */
	public function getNumComments($articleId)
	{				
		return Epic::getModel('blog/comment')
					 ->where('article_id', array('eq' =>(int) $articleId))
					 ->count();
	}
	
	/**
	 * Get a Article by id
	 * 
	 * @return Epic_Db_Model_Object a row retrive form database
	 */
	public function getArticleById($articleId)
	{
		if($articleId)
			return Epic::getModel('blog/article')->load($articleId);
	}
	
	/**
	 * Get all comment by article id
	 * 
	 * @return Epic_Db_Collection all row retrive form database
	 */
	public function getCommentsByArticleId($articleId)
	{
		if($articleId)
			return Epic::getModel('blog/comment')->getCollection()
			->join('account_user AS u', 'm.user_id = u.user_id', array('username' =>'u.username', 'avatar' => 'u.avatar'))
			->where('article_id', array('eq' => $articleId))
			->load();
	}
	
	/**
	 * Get all active categories
	 */
	public function getAllActiveCategories()
	{
		$categoryBlock = new Blog_Block_Category();
		return $categoryBlock->getAllActiveCategories();
	}
}